#include "KieperException.hpp"

#include <ostream>

#include <boost/format.hpp>


namespace kieper {
    
    using std::ostream;

    using boost::format;
    
    KieperException::KieperException(const char* msg, const char* file, 
            unsigned int line, char* reason) :
        msg_(msg), file_(file), line_(line), reason_(reason) {}
    
    KieperException::KieperException(const char* msg, char* reason) :
        msg_(msg), file_(0), line_(0), reason_(reason) {} 
    
    KieperException::~KieperException() {}
    
    void KieperException::setReason(char* reason) {
        reason_ = reason;
    }

    const char* KieperException::longMsg() {
        // TODO: refactor into something simpler
        if (reason_ != NULL) {
            if (file_ != 0 && line_ !=0) {
                return (format("%s:%d %s, reason: %s") % file_ % line_ % msg_ %
                    reason_).str().c_str();
            } else if (file_ != 0) {
                return (format("%s %s, reason: %s") % file_ % msg_ % 
                    reason_).str().c_str();
            } else {
                return (format("%s, reason: %s") % msg_ % reason_).str().c_str(); 
            }
        } else {
            if (file_ != 0 && line_ !=0) {
                return (format("%s:%d %s") % file_ % line_ % msg_).str().c_str();
            } else if (file_ != 0) {
                return (format("%s %s") % file_ % msg_).str().c_str();
            } else {
                return (format("%s") % msg_).str().c_str(); 
            }
        }
    }

    ostream& operator<<(ostream& output, const KieperException& e) {
        output << "sss";
    }
}
